#!coding=utf-8
from standard_function import *

def del_appdown_task(driver):
    #清空下载任务
    try:
       btn = WaitforElementById(driver,'com.diguayouxi:id/menu_download',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        btn.click()

    while True:
        try:
            tasks=WaitforElementsById(driver,"com.diguayouxi:id/check",30)
        except NoSuchElementException:
            break
        else:
            tasks[0].click()
            time.sleep(1)
            try:
                right_btn=WaitforElementById(driver,'com.diguayouxi:id/selection_btn','5')
                delel_btn=WaitforElementById(driver,'com.diguayouxi:id/menu_delete',5)
            except NoSuchElementException:
                raise NoSuchElementException
            else:
                right_btn.click()
                delel_btn.click()

    driver.back()

def clickappdown(driver):
    try:
        list_f=WaitforElementById(driver,'com.diguayouxi:id/list',30)
    except NoSuchElementException:
        raise NoSuchElementException
    try:
        btns=WaitforElementsByClass(list_f,'android.widget.FrameLayout',30)
    except NoSuchElementException:
        raise  NoSuchElementException
    else:
        print len(btns)
        for item in btns:
            try:
                size=WaitforElementById(item,"com.diguayouxi:id/size",30).get_attribute("text")
            except NoSuchElementException:
                continue
            print size
            if 'G' not in size:
                if 'K' in size or float(size[0:-1])<10:
                    continue
            try:
                btn=WaitforElementById(item,'com.diguayouxi:id/btn_list',5)
            except NoSuchElementException:
                continue
            if btn.get_attribute("enabled")=='true':
                btn.click()
                try:
                    cancel=WaitforElementById(driver,'com.diguayouxi:id/negative_button',5)
                except NoSuchElementException:
                    pass
                else:
                    cancel.click()
                time.sleep(2)
                btn.click()

def game(driver):
    categorys = [u'角色扮演',u'动作游戏',u'策略塔防',u'射击游戏',u'益智休闲',u'卡牌',u'沙盒游戏',u'体育运动',u'竞速游戏',u'冒险解谜',u'模拟经营',u'音乐游戏',u'养成游戏']
    try:
        WaitforElementByName(driver,u"分类",30).click()
    except NoSuchElementException:
        raise  NoSuchElementException
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for i in categorys:
        print "Current category is:",i
        try:
            btn=WaitforElementByName(driver,i,5)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            try:
                clickappdown(driver)
            except NoSuchElementException:
                continue
            driver.back()

def run():
    desired_caps = {}
    #desired_caps['automationName'] = 'uiautomator2'
    desired_caps['platformName'] = "Android"               # 声明是ios还是Android系统
    desired_caps['platformVersion'] = ANDROID_VERSION             # Android内核版本号，也可以在模拟器设置中查看
    desired_caps['deviceName'] = DEVICE         # 连接的设备名称，可通过adb devices查看
    desired_caps['appPackage'] = 'com.diguayouxi'    # apk的包名
    desired_caps['appActivity'] = '.ui.FirstActivity'          # apk的launcherActivity
    desired_caps['noReset']='true'                         #不需要再次安装apk
    driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)          # 建立 session
    time.sleep(3)
    #driver.find_element_by_name().tag_name
    del_appdown_task(driver)
    #soft(driver)
    #time.sleep(2)
    game(driver)
    driver.quit()

#run()